package com.gridmobi.xrtb.model.rtb;


import com.gridmobi.xrtb.common.GeoMap;
import com.gridmobi.xrtb.domain.adn.OsE;
import com.gridmobi.xrtb.handler.ImprOffersHandler;
import com.gridmobi.xrtb.model.openrtb23.Imp;
import com.gridmobi.xrtb.model.openrtb23.OpenRTB23BidRequest;
import com.gridmobi.xrtb.util.DOMUtils;
import com.gridmobi.xrtb.util.RtbUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 */

public class SmaatoAdmTemplate {
    private static final Logger logger = LoggerFactory.getLogger(SmaatoAdmTemplate.class);

    private static String IMAGEAD_TEMPLATE = "" +
            "<ad xmlns:xsi=\\\"http://www.w3.org/2001/XMLSchema-instance\\\" xsi:noNamespaceSchemaLocation=\\\"smaato_ad_v0.9.xsd\\\" modelVersion=\\\"0.9\\\">" +
            "<imageAd>" +
            "<clickUrl>__CLICKURL__</clickUrl>" +
            "<imgUrl>__IMAGEURL__</imgUrl>" +
            "<width>{creative_ad_width}px</width>" +
            "<height>{creative_ad_height}px</height>" +
            "<toolTip>__TOOLTIP__</toolTip>" +
            "<additionalText>__ADDITIONALTEXT__</additionalText>" +
            "<beacons>" +
            "<beacon>__PIXELURL__</beacon>" +
            "</beacons>" +
            "</imageAd>" +
            "</ad>";

    private static String TEXTAD_TEMPLATE = "" +
            "<ad>" +
            "<textAd>" +
            "<clickUrl>__CLICKURL__</clickUrl>" +
            "<clickTextl>__TEXT__</imgUrl>" +
            "<toolTip>__TOOLTIP__</toolTip>" +
            "<additionalText>__ADDITIONALTEXT__</additionalText>" +
            "<beacons>" +
            "<beacon>__PIXELURL__</beacon>" +
            "</beacons>" +
            "</textAd>" +
            "</ad>";

    private static String RICHMEDIA_TEMPLATE = "" +
            "<ad xmlns:xsi=\\\"http://www.w3.org/2001/XMLSchema-instance\\\" xsi:noNamespaceSchemaLocation=\\\"http://standards.smaato.com/ad/smaato_ad_v0.9.xsd\\\" modelVersion=\\\"0.9\\\">" +
            "<richmediaAd>" +
            "<content>" +
            "<![CDATA[ __JAVASCRIPT__ ]]>" +
            "</content>" +
            "<width>{creative_ad_width}</width>" +
            "<height>{creative_ad_height}</height>" +
            "<beacons>" +
            "<beacon>__PIXELURL__</beacon>" +
            //  "<beacon>__RICHMEDIABEACON__</beacon>" +
            "</beacons>" +
            "</richmediaAd>" +
            "</ad>";

    public static String createSmaatoRichMediaTpl(Campaign winCampaign, String script, String impUrl, String clickUrl, String impr2URL) {


        Document document = DOMUtils.newXMLDocument();

        Element root = document.createElement("ad");
        document.appendChild(root);
        root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.setAttribute("xsi:noNamespaceSchemaLocation", "smaato_ad_v0.9.xsd");
        root.setAttribute("modelVersion", "0.9");
        Element richmediaAd = document.createElement("richmediaAd");
        root.appendChild(richmediaAd);

        Element content = document.createElement("content");
        System.out.println(script);

        content.appendChild((document.createCDATASection(script)));
        Element height = document.createElement("height");
        height.appendChild(document.createTextNode(String.valueOf(winCampaign.getCreative().getH())));
        Element width = document.createElement("width");
        width.appendChild(document.createTextNode(String.valueOf(winCampaign.getCreative().getW())));

        richmediaAd.appendChild(content);
        richmediaAd.appendChild(height);
        richmediaAd.appendChild(width);
        Element beacons = document.createElement("beacons");

        Element beacon1 = document.createElement("beacon");
        if (StringUtils.isNotBlank(impr2URL)) {
            beacon1.appendChild(document.createTextNode(impr2URL));
        } else {
            beacon1.appendChild(document.createTextNode(impUrl));
        }

        beacons.appendChild(beacon1);
        richmediaAd.appendChild(beacons);

        String xmlStr = DOMUtils.toStringFromDoc(document);
        return xmlStr.trim();

    }

    public static String createSmaatoImgTpl(int script, String rtbname, Imp imp, Campaign winCampaign, String clickUrl, String impUrl, String impr2URL, OpenRTB23BidRequest bidRequest) {


        Document document = DOMUtils.newXMLDocument();

        Element root = document.createElement("ad");
        document.appendChild(root);
        root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.setAttribute("xsi:noNamespaceSchemaLocation", "smaato_ad_v0.9.xsd");
        root.setAttribute("modelVersion", "0.9");
        Element image = document.createElement("imageAd");
        root.appendChild(image);

        Element click = document.createElement("clickUrl");

        click.appendChild(document.createTextNode(clickUrl));
        Element img = document.createElement("imgUrl");
        img.appendChild((document.createTextNode(RtbUtil.packCreativeUrl(1, winCampaign.getCreative().getUrl()))));
        Element height = document.createElement("height");
        height.appendChild(document.createTextNode(String.valueOf(winCampaign.getCreative().getH())));
        Element width = document.createElement("width");
        width.appendChild(document.createTextNode(String.valueOf(winCampaign.getCreative().getW())));
        Element beacons = document.createElement("beacons");
        image.appendChild(click);
        image.appendChild(img);
        image.appendChild(height);
        image.appendChild(width);
        boolean isScriptImp = false;
        if (script == 1) {
            String geo = RtbUtil.get23Country(bidRequest);
            String osV = RtbUtil.get23OS(bidRequest);
            String geo2 = GeoMap.word3Map.get(geo);
            String osKey = OsE.getKeyByV(osV);

            String sdkOfferKey = geo2 + osKey;
            if (ImprOffersHandler.GEO_OS_MAP.containsKey(sdkOfferKey)) {
                String url = "https://rtbsg.mobicoca.com/cimpr";
                String urleu = "https://rtbeu.mobicoca.com/cimpr";
                boolean isEu = AdmTemplate.EU_GEOS.contains(bidRequest.getDspTrackLog().getGeo());
                Element beacon1 = document.createElement("beacon");
                beacon1.appendChild(document.createTextNode((isEu ? urleu : url) + "?g=" + geo + "&t=" + osV + "&sp=1&r=" + RandomStringUtils.randomAlphanumeric(4)));
                beacons.appendChild(beacon1);
                isScriptImp = true;

            }
        }
        if (!isScriptImp) {

        }

        image.appendChild(beacons);
        String xmlStr = DOMUtils.toStringFromDoc(document);
        return xmlStr.trim();

    }


    public static void main(String[] args) {
        Campaign campaign = new Campaign();
        campaign.setId(1L);
        Creative creative = new Creative();
        creative.setW(100);
        creative.setH(200);
        creative.setUrl("http://asdasdasdas");
        campaign.setCreative(creative);
        createSmaatoImgTpl(1, "", new Imp(), campaign, "clickurl", "imprurl1", "impreurl2", null);
    }
}

